package com.fenghongzhang.kuaishou.index.home;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fenghongzhang.kuaishou.R;
import com.fenghongzhang.kuaishou.index.home.adapter.HomeViewPagerAdapter;
import com.fenghongzhang.kuaishou.index.home.view.fragment.AttentFragment;
import com.fenghongzhang.kuaishou.index.home.view.fragment.FindFragment;
import com.fenghongzhang.kuaishou.index.home.view.fragment.OptimalFragment;
import com.fenghongzhang.kuaishou.mvp.login.view.LoginActivity;
import com.fenghongzhang.kuaishou.utils.ActivityStatusBarUtil;
import com.fenghongzhang.kuaishou.utils.DpTools;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.android.material.tabs.TabLayout;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * home页中的主要fragment
 */
public class IndexFragment extends Fragment implements View.OnClickListener , OnTabSelectListener  {

    private static final String TAG = "IndexFragment";
    private Button login;
    private SlidingTabLayout indexTab;
    private ImageView homeBarSearch;
    private ImageView slideMenu;
    private ViewPager indexViewPager;
    ArrayList<Fragment> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_index, container, false);
        initView(inflate);
        initData();
        return inflate;
    }

    private void initView(View inflate) {
        login = (Button) inflate.findViewById(R.id.login);
        slideMenu = (ImageView) inflate.findViewById(R.id.slideMenu);
        indexTab = (SlidingTabLayout) inflate.findViewById(R.id.sliding_tab);
        homeBarSearch = (ImageView) inflate.findViewById(R.id.home_bar_search);
        indexViewPager = (ViewPager) inflate.findViewById(R.id.index_viewPager);
        login.setOnClickListener(this);
        slideMenu.setOnClickListener(this);
        indexTab.setOnTabSelectListener(this);

    }


    private void initData() {
        list = new ArrayList<>();
        list.add(new AttentFragment());
        list.add(new FindFragment());
        list.add(new OptimalFragment());

        HomeViewPagerAdapter homeViewPagerAdapter = new HomeViewPagerAdapter(getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,list);
        indexViewPager.setOffscreenPageLimit(2);
        indexViewPager.setAdapter(homeViewPagerAdapter);
        indexTab.setViewPager(indexViewPager,new String[]{"关注","发现","精选"});


        indexViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                //选中tabLayout
                if(position == list.size()-1){
                    ActivityStatusBarUtil.setAndroidNativeLightStatusBar(getActivity(),false);
                }else{
                    //如果想让屏幕沾满全屏,记得删除style文件的window属性
                    ActivityStatusBarUtil.setAndroidNativeLightStatusBar(getActivity(),true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.slideMenu:
                DrawerLayout drawerLayout = getActivity().findViewById(R.id.drawerLayout);
                drawerLayout.openDrawer(Gravity.LEFT);
                break;
        }
    }

    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }
}

